package com.example.weather_plus.data.repository;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.example.weather_plus.data.model.AQIInfo;
import com.example.weather_plus.data.model.DailyForecastInfo;
import com.example.weather_plus.data.model.HourlyForecastInfo;
import com.example.weather_plus.data.model.WeatherInfo;
import com.example.weather_plus.data.network.WeatherApiService;
import com.example.weather_plus.data.retrofit.RetrofitClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class WeatherRepository {
    private final WeatherApiService apiService;

    public WeatherRepository() {
        String baseUrl = "https://api.qweather.com/v7/";
        RetrofitClient client = new RetrofitClient();
        apiService = client.getInstance(baseUrl).create(WeatherApiService.class);
    }

    public LiveData<WeatherInfo> fetchCurrentWeather(String locationId, String apiKey) {
        final MutableLiveData<WeatherInfo> data = new MutableLiveData<>();
        apiService.getNowWeather(locationId, apiKey).enqueue(new Callback<WeatherInfo>() {
            @Override
            public void onResponse(@NonNull Call<WeatherInfo> call, @NonNull Response<WeatherInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    data.postValue(response.body());
                } else {
                    // Handle error, e.g., set error state in LiveData
                }
            }

            @Override
            public void onFailure(Call<WeatherInfo> call, Throwable t) {
                // Handle failure, e.g., set error state in LiveData
            }
        });
        return data;
    }

    public LiveData<DailyForecastInfo> fetchForecast(String locationId, String apiKey) {
        final MutableLiveData<DailyForecastInfo> data = new MutableLiveData<>();
        apiService.getForecast(locationId, apiKey).enqueue(new Callback<DailyForecastInfo>() {
            @Override
            public void onResponse(Call<DailyForecastInfo> call, Response<DailyForecastInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    data.postValue(response.body());
                } else {
                    // Handle error
                }
            }

            @Override
            public void onFailure(Call<DailyForecastInfo> call, Throwable t) {
                // Handle failure
            }
        });
        return data;
    }

    public LiveData<HourlyForecastInfo> fetchHourlyForecast(String query, String apiKey) {
        final MutableLiveData<HourlyForecastInfo> data = new MutableLiveData<>();
        apiService.getHourlyForecast(query, apiKey).enqueue(new Callback<HourlyForecastInfo>() {
            @Override
            public void onResponse(Call<HourlyForecastInfo> call, Response<HourlyForecastInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    data.postValue(response.body());
                } else {
                    // Handle error
                }
            }

            @Override
            public void onFailure(Call<HourlyForecastInfo> call, Throwable t) {
                // Handle failure
            }
        });
        return data;
    }

    public LiveData<AQIInfo> fetchAQI(String locationId, String apiKey) {
        final MutableLiveData<AQIInfo> data = new MutableLiveData<>();
        apiService.getAQI(locationId, apiKey).enqueue(new Callback<AQIInfo>() {
            @Override
            public void onResponse(Call<AQIInfo> call, Response<AQIInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    data.postValue(response.body());
                } else {
                    // Handle error
                }
            }

            @Override
            public void onFailure(Call<AQIInfo> call, Throwable t) {
                // Handle failure
            }
        });
        return data;
    }

}